﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Actuals.Common
{
    public static class RSAEncryptDecrypt
    {
        public static string Encrypt(string data)
        {
            string _encryptedText = string.Empty;
            try
            {

                UnicodeEncoding ByteConverter = new UnicodeEncoding();


                byte[] dataToEncrypt = ByteConverter.GetBytes(data);
                byte[] encryptedData;

                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {


                    encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);


                    _encryptedText = ByteConverter.GetString(encryptedData);


                }
            }
            catch (ArgumentNullException)
            {
                //Catch this exception in case the encryption did
                //not succeed.
                //Console.WriteLine("Encryption failed.");

            }

            return _encryptedText;
        }

        public static string Decrypt(string data)
        {
            string _decryptedText = string.Empty;
            try
            {
                //Create a UnicodeEncoder to convert between byte array and string.
                UnicodeEncoding ByteConverter = new UnicodeEncoding();

                //Create byte arrays to hold original, encrypted, and decrypted data.
                byte[] dataToDecrypt = ByteConverter.GetBytes(data);
                byte[] decryptedData;


                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {


                    decryptedData = RSADecrypt(dataToDecrypt, RSA.ExportParameters(true), false);
                    _decryptedText = ByteConverter.GetString(decryptedData);


                }
            }
            catch (ArgumentNullException)
            {
                //Catch this exception in case the encryption did
                //not succeed.
                //Console.WriteLine("Encryption failed.");

            }

            return _decryptedText;
        }


        static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
        {
            try
            {
                byte[] encryptedData;
                //Create a new instance of RSACryptoServiceProvider.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {

                    //Import the RSA Key information. This only needs
                    //toinclude the public key information.
                    RSA.ImportParameters(RSAKeyInfo);

                    //Encrypt the passed byte array and specify OAEP padding.  
                    //OAEP padding is only available on Microsoft Windows XP or
                    //later.  
                    encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                }
                return encryptedData;
            }
            //Catch and display a CryptographicException  
            //to the console.
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return null;
            }

        }

        static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
        {
            try
            {
                byte[] decryptedData;
                //Create a new instance of RSACryptoServiceProvider.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    //Import the RSA Key information. This needs
                    //to include the private key information.
                    RSA.ImportParameters(RSAKeyInfo);

                    //Decrypt the passed byte array and specify OAEP padding.  
                    //OAEP padding is only available on Microsoft Windows XP or
                    //later.  
                    decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                }
                return decryptedData;
            }
            //Catch and display a CryptographicException  
            //to the console.
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());

                return null;
            }

        }



    }
}

